package com.petmarket.controller;

import com.alibaba.fastjson.JSON;
import com.petmarket.client.LoginClient;
import com.petmarket.client.UserClient;
import com.petmarket.common.LogId;
import com.petmarket.common.PageResult;
import com.petmarket.common.Result;
import com.petmarket.entity.LoginDetail;
import com.petmarket.entity.Return;
import com.petmarket.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigInteger;
import java.util.List;

@RestController
@RequestMapping("/LoginDetails")
@Slf4j
public class LoginController {
    @Autowired
    private LoginClient loginClient;

    @Autowired
    private UserClient userClient;

    @PostMapping("/insert")
    public Result<Boolean> insert(@RequestBody LoginDetail loginDetail) {
        try {
            if (log.isInfoEnabled()) {
                log.info("LoginController.insert:{}", JSON.toJSONString(loginDetail));
            }

            BigInteger id = LogId.getId();

            loginDetail.setId(id);
            loginClient.insert(loginDetail);

            User user=new User();
            user.setAccount(loginDetail.getAccount());
            user.setBalance("5000");
            user.setName(LogId.usingUUID().substring(0,8));
            user.setIsDeleted("0");
            user.setId(id);

            userClient.insert(user);

            return Result.ok("插入成功");
        } catch (Exception e) {
            return Result.fail("插入失败");
        }
    }


    @PostMapping("/insertManager")
    public Result<Boolean> insertManager(@RequestBody LoginDetail loginDetail) {
        try {
            if (log.isInfoEnabled()) {
                log.info("LoginController.insert:{}", JSON.toJSONString(loginDetail));
            }

            BigInteger id = LogId.getId();
            loginDetail.setUserType("3");
            loginDetail.setId(id);

            loginClient.insert(loginDetail);

            User user=new User();
            user.setAccount(loginDetail.getAccount());
            user.setBalance("5000");
            user.setName(LogId.usingUUID().substring(0,8));
            user.setIsDeleted("0");
            user.setId(id);

            userClient.insert(user);

            return Result.ok("插入成功");
        } catch (Exception e) {
            return Result.fail("插入失败");
        }
    }



    @PostMapping("/query")
    public Result<LoginDetail> query(@RequestBody LoginDetail loginDetail) {
        try {
            if (log.isInfoEnabled()) {
                log.info("LoginController.query:{}", JSON.toJSONString(loginDetail));
            }
            LoginDetail res = loginClient.query(loginDetail);
            return Result.ok(res);
        } catch (Exception e) {
            return Result.fail("查询失败");
        }
    }

    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody LoginDetail loginDetail) {
        try {
            if (log.isInfoEnabled()) {
                log.info("LoginController.update:{}", JSON.toJSONString(loginDetail));
            }
            loginClient.update(loginDetail);
            return Result.ok("更新成功");
        } catch (Exception e) {
            return Result.fail("更新失败");
        }
    }

    @PostMapping("/delete")
    public Result<Boolean> delete(@RequestBody LoginDetail loginDetail) {
        try {
            if (log.isInfoEnabled()) {
                log.info("LoginController.delete:{}", JSON.toJSONString(loginDetail));
            }
            loginClient.remove(loginDetail);
            return Result.ok("删除成功");
        } catch (Exception e) {
            return Result.fail("删除失败");
        }
    }

    @PostMapping("/check")
    public Result<Return> check(@RequestBody LoginDetail loginDetail) {
        try {
            if(log.isInfoEnabled()){
                log.info("LoginController.check:{}",JSON.toJSONString(loginDetail));
            }
            List<String> res = loginClient.check(loginDetail);
            Return re = new Return();
            re.setType(Integer.valueOf(res.get(0)));
            re.setId(Integer.valueOf(res.get(1)));
            return Result.ok(re);
        } catch (Exception e) {
            return Result.fail("0");
        }
    }

    @PostMapping("/getLoginPage")
    public Result<PageResult<LoginDetail>> getUserPage(@RequestBody LoginDetail loginDetail){
        try{
            if (log.isInfoEnabled()){
                log.info("LoginController.getLoginPage:{}", JSON.toJSONString(loginDetail));
            }
            PageResult<LoginDetail> result = loginClient.getLoginPage(loginDetail.getPageNo(),loginDetail.getPageSize());

            result.setTotal(loginDetail.getPageSize());
            return Result.ok(result);
        }
        catch (Exception e){
            return Result.fail("获取列表失败");
        }
    }
}
